#include "StdAfx.h"
#include "http_download_thread.h"
#include "chat_msg_manager.h"
#include "dll_object.h"
#include "dlg_imcc.h"

http_download_thread::http_download_thread(void)
{
}

http_download_thread::~http_download_thread(void)
{
}

void http_download_thread::OnProcMsgQueueStart()
{
}

void http_download_thread::LoopProc(download_pack* pMsgPack, PWORKER_PARAMETER work_para)
{
	if(pMsgPack->imcc == NULL || !::IsWindow(pMsgPack->imcc))
		return;

	aot::tt::iobj_auto_ptr<aot::file_trans::ifile_trans_client> client;
	get_interface()->create_file_trans_client(client);

	aot::tt::iobj_auto_ptr<aot::file_trans::isession> session;
	client->create_session(session);
	if ( session.get() )
	{
		session->set_type(aot::file_trans::isession::e_direct_download_file);
		session->set_url(pMsgPack->str_url);
		session->set_file_name(pMsgPack->str_file_name);
	}

	if ( client.get() && session.get() )
	{
		if( !client->syn_process(session.get()) )
		{
			AOT_TRACE(AOT_LM_INFO, "download: url: %s, file_name: %s fail!", 
				pMsgPack->str_url, pMsgPack->str_file_name);
			TRACE("download: url: %s, file_name: %s fail!", 
				pMsgPack->str_url, pMsgPack->str_file_name);
			::SendMessage(pMsgPack->imcc, dlg_imcc::WM_DOWNLOAD_IMG_FAIL, NULL, (LPARAM)session.get());

			return;
		}
	}

	if(pMsgPack->imcc == NULL || !::IsWindow(pMsgPack->imcc))
		return;

	chat_msg_manager::img_replace_t t;
	t.old_img = pMsgPack->str_url;
	t.new_img = pMsgPack->str_file_name;

	AOT_TRACE(AOT_LM_DEBUG, "download: url: %s, file_name: %s success!", 
		pMsgPack->str_url, pMsgPack->str_file_name);
	TRACE("download: url: %s, file_name: %s success!", 
		pMsgPack->str_url, pMsgPack->str_file_name);

	::SendMessage(pMsgPack->imcc, dlg_imcc::WM_REPLACE_IMG_MSG, NULL, (LPARAM)&t);
}


BOOL http_download_thread::push_message(CString str_url, CString str_file_name, HWND imcc)
{
	download_pack *ptr_pack = new download_pack;	/// handled del	 

	if( NULL == ptr_pack )
	{
		return FALSE;
	}

	ptr_pack->imcc = imcc;
	ptr_pack->str_file_name = str_file_name;
	ptr_pack->str_url = str_url;

	Add(ptr_pack);

	return TRUE;
}

BOOL 
http_download_thread::OnWorkerStart(PWORKER_PARAMETER work_para)
{
	AOT_TRACE(AOT_LM_INFO, "......");
	return TRUE;
}

BOOL 
http_download_thread::OnWorkerEnd(PWORKER_PARAMETER work_para)
{
	AOT_TRACE(AOT_LM_INFO, "......");
	return TRUE;
}